package com.ruoyi.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.order.domain.OrderCenter;
import com.ruoyi.order.domain.PrintOrder;
import com.ruoyi.order.service.OrderCenterService;
import com.ruoyi.order.service.PrintOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单中心服务实现类
 */
@Service
public class OrderCenterServiceImpl implements OrderCenterService {

    @Autowired
    private PrintOrderService printOrderService;

    // TODO: 注入商城订单服务
    // @Autowired
    // private MallOrderService mallOrderService;

    @Override
    public PageInfo<OrderCenter> getUserAllOrders(Long userId, int pageNum, int pageSize) {
        return getUserOrdersByStatus(userId, null, pageNum, pageSize);
    }

    @Override
    public PageInfo<OrderCenter> getUserOrdersByStatus(Long userId, String status, int pageNum, int pageSize) {
        List<OrderCenter> allOrders = new ArrayList<>();
        
        // 获取打印订单
        PrintOrder printQuery = new PrintOrder();
        printQuery.setUserId(userId);
        if (status != null) {
            printQuery.setStatus(status);
        }
        PageInfo<PrintOrder> printOrders = printOrderService.pageList(printQuery, 1, pageSize * 2);
        
        // 转换为统一模型
        for (PrintOrder printOrder : printOrders.getList()) {
            OrderCenter orderCenter = convertToOrderCenter(printOrder);
            allOrders.add(orderCenter);
        }
        
        // TODO: 获取商城订单
        // if (mallOrderService != null) {
        //     // 获取商城订单逻辑
        // }
        
        // 按创建时间排序
        allOrders.sort(Comparator.comparing(OrderCenter::getCreateTime).reversed());
        
        // 手动分页
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, allOrders.size());
        List<OrderCenter> pageList = allOrders.subList(start, end);
        
        // 构建分页信息
        PageInfo<OrderCenter> pageInfo = new PageInfo<>(pageList);
        pageInfo.setTotal(allOrders.size());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPages((int) Math.ceil((double) allOrders.size() / pageSize));
        
        return pageInfo;
    }

    @Override
    public PageInfo<OrderCenter> getUserOrdersByType(Long userId, String orderType, int pageNum, int pageSize) {
        if ("print".equals(orderType)) {
            // 只查询打印订单
            PrintOrder printQuery = new PrintOrder();
            printQuery.setUserId(userId);
            PageInfo<PrintOrder> printOrders = printOrderService.pageList(printQuery, pageNum, pageSize);
            
            List<OrderCenter> orderCenters = printOrders.getList().stream()
                    .map(this::convertToOrderCenter)
                    .collect(Collectors.toList());
            
            PageInfo<OrderCenter> pageInfo = new PageInfo<>(orderCenters);
            pageInfo.setTotal(printOrders.getTotal());
            pageInfo.setPageNum(printOrders.getPageNum());
            pageInfo.setPageSize(printOrders.getPageSize());
            pageInfo.setPages(printOrders.getPages());
            
            return pageInfo;
        } else if ("mall".equals(orderType)) {
            // TODO: 只查询商城订单
            // return mallOrderService.getUserOrders(userId, pageNum, pageSize);
            return new PageInfo<>();
        } else {
            // 查询所有类型
            return getUserAllOrders(userId, pageNum, pageSize);
        }
    }

    /**
     * 将打印订单转换为订单中心统一模型
     */
    private OrderCenter convertToOrderCenter(PrintOrder printOrder) {
        OrderCenter orderCenter = new OrderCenter();
        orderCenter.setId(printOrder.getId());
        orderCenter.setOrderNo(printOrder.getOrderNo());
        orderCenter.setUserId(printOrder.getUserId());
        orderCenter.setTitle(printOrder.getTitle());
        orderCenter.setDescription(printOrder.getDescription());
        orderCenter.setAmount(printOrder.getPayAmount() != null ? printOrder.getPayAmount() : printOrder.getTotalAmount());
        orderCenter.setStatus(printOrder.getStatus());
        orderCenter.setOrderType("print");
        orderCenter.setPayTime(printOrder.getPayTime());
        orderCenter.setFinishTime(printOrder.getFinishTime());
        orderCenter.setCancelTime(printOrder.getCancelTime());
        orderCenter.setCreateTime(printOrder.getCreateTime());
        orderCenter.setUpdateTime(printOrder.getUpdateTime());
        
        // 设置显示文本
        orderCenter.setStatusText(getStatusText(printOrder.getStatus()));
        orderCenter.setOrderTypeText("打印订单");
        
        return orderCenter;
    }

    /**
     * 获取状态显示文本
     */
    private String getStatusText(String status) {
        if (status == null) return "";
        
        switch (status) {
            case "pending":
                return "待支付";
            case "paid":
                return "已支付";
            case "cancelled":
                return "已取消";
            case "finished":
                return "已完成";
            default:
                return status;
        }
    }
} 